﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Manager.Tools;

namespace Web.Areas.Admin.Controllers
{
    public class HomeController : Controller
    {
        private readonly Manager.BLL.Admin.M_ModuleBll bll = new Manager.BLL.Admin.M_ModuleBll();
        private readonly Manager.BLL.Admin.M_AdminBll bllAdmin = new Manager.BLL.Admin.M_AdminBll();
        //
        // GET: /Admin/Home/

        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult Header()
        {
            return PartialView();
        }
        public PartialViewResult Footer()
        {
            return PartialView();
        }
        /// <summary>
        /// 左侧菜单
        /// </summary>
        /// <returns></returns>
        public ActionResult Menu()
        {
            IList<Models.MenuItem> menus=new List<Models.MenuItem>();
            foreach (var item in bll.GetModelList("IsValid=1 and ParentId=0 Order by Sort"))
            {
                Models.MenuItem menu = new Models.MenuItem();
                menu.MenuID = item.ModuleId;
                menu.MenuName = item.ModuleName;
                menu.MenuCode = item.ModuleCode;
                menu.MenuHref = item.ModuleUrl;
                foreach (var child in bll.GetModelList("IsValid=1 and ParentId=" + menu.MenuID + " Order By Sort"))
                {
                    Models.MenuItem childMenu = new Models.MenuItem();
                    childMenu.MenuID = child.ModuleId;
                    childMenu.MenuName = child.ModuleName;
                    childMenu.MenuCode = child.ModuleCode;
                    childMenu.MenuHref = child.ModuleUrl;
                    menu.ChildMenus.Add(childMenu);
                }
                menus.Add(menu);
            }
            return View(menus);
        }

        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 后台登录
        /// </summary>
        /// <param name="forms"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(FormCollection forms)
        {
            string username = PageValidate.InputText(forms["username"].Trim(), 20);
            string password = PageValidate.InputText(forms["password"].Trim(), 32);

            Manager.Model.Admin.M_Admin loginAdmin = bllAdmin.ValidateLogin(username, password);

            if (loginAdmin != null)
            {
                Session["administrator"] = loginAdmin;

                return RedirectToAction("Index", "Home");
            }
            else
            {
                ViewBag.Error = "用户登录失败！";

            }
            return View();
        }
        /// <summary>
        /// 后台登出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult LogOut()
        {
            Session.Remove("administrator");
            return RedirectToAction("Login", "Home");
        }
    }
}
